﻿package  com.yy.tools.keyboard {
    import flash.events.*;
    import flash.display.*;

    public class KeyCombo extends MovieClip {

        public static var COMBO_PRESSED:String = "combo_pressed";
        public static var COMBO_RELEASED:String = "combo_released";
        public static var COMBO_REPEAT:String = "combo_repeat";

        private var keys:Array;
        public var singleKey:Key;
        private var comboActivated:Boolean;

        public function KeyCombo(){
            this.comboActivated = false;
            this.keys = new Array();
        }
        public function addKey(key:Key):void{
            this.keys.push(key);
            this.singleKey = key;
        }
        public function getComboActivated():Boolean{
            return (this.comboActivated);
        }
        private function analyzeState():void{
            var _local3:Key;
            var _local1:Boolean;
            var _local2:int;
            while (_local2 < this.keys.length) {
                _local3 = this.keys[_local2];
                if (!_local3.getIsDown()){
                    _local1 = false;
                    break;
                };
                _local2++;
            };
            if (((_local1) && (!(this.comboActivated)))){
                this.comboActivated = true;
                dispatchEvent(new Event(COMBO_PRESSED));
            } else {
                if (((_local1) && (this.comboActivated))){
                    dispatchEvent(new Event(COMBO_REPEAT));
                } else {
                    if (((!(_local1)) && (this.comboActivated))){
                        this.comboActivated = false;
                        dispatchEvent(new Event(COMBO_RELEASED));
                    };
                };
            };
        }
        public function keyDown(id:int):void{
            var _local4:Key;
            var _local2:Boolean;
            var _local3:int;
            while (_local3 < this.keys.length) {
                _local4 = this.keys[_local3];
                if (_local4.getKeyId() == id){
                    _local4.setIsDown(true);
                    _local2 = true;
                    break;
                };
                _local3++;
            };
            if (_local2){
                this.analyzeState();
            };
        }
        public function keyUp(_arg1:int):void{
            var _local4:Key;
            var _local2:Boolean;
            var _local3:int;
            while (_local3 < this.keys.length) {
                _local4 = this.keys[_local3];
                if (_local4.getKeyId() == _arg1){
                    _local4.setIsDown(false);
                    _local2 = true;
                    break;
                };
                _local3++;
            };
            if (_local2){
                this.analyzeState();
            };
        }
    }
}
